package com.jzg.sys.action;

import com.jzg.sys.common.rest.BaseRest;
import com.jzg.sys.common.vo.ResultVO;
import com.jzg.sys.dao.entity.SecMenu;
import com.jzg.sys.exception.BusinessException;
import com.jzg.sys.manager.SecMenuManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 菜单
 * @author:asxx 李志鹏
 * @create:2019-02-28 15:36
 */
@Controller
@RequestMapping(value = "/menuAction")
public class MenuAction extends BaseRest {
    @Autowired
    private SecMenuManager menuManager;

    @RequestMapping(value = "/findTree")
    @ResponseBody
    public List<SecMenu> findTree(String parentId,String sysCode) {
        List<SecMenu> list = menuManager.findMenuTree(parentId, sysCode);
        return list;
    }

    @RequestMapping(value = "/findById")
    @ResponseBody
    public SecMenu findById(String id) {
        return menuManager.getById(id);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public ResultVO save(SecMenu secMenu) {
        try {
            secMenu.setIsValid(true);
            if(secMenu.getMenuId().startsWith("new")){
                menuManager.insertMenu(secMenu);
            }else {
                menuManager.updateById(secMenu);
            }
            return new ResultVO();
        } catch (BusinessException be) {
            be.printStackTrace();
            return new ResultVO(false, be.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ResultVO(false, "保存出错");
        }
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public ResultVO del(String menuId) {
        try {
            menuManager.delMenu(menuId);
            return new ResultVO();
        } catch (BusinessException be) {
            be.printStackTrace();
            return new ResultVO(false, be.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ResultVO(false, "保存出错");
        }
    }

}
